package interaction.sampling.matching;

import interaction.sampling.Sample;
import interaction.sampling.SampleSet;
import interaction.sampling.Transform;

/**
 * Measures similarity in the length of consequent monotone segments in a test spectrum
 * as compared to the average of a set of comparison samples' spectra
 */
public class LevelLengthMatcher implements Matcher {
	public double match(Sample test, SampleSet comparison)
	{
		Transform t = getDeviationTransform();
		comparison = comparison.apply(t);
		test = t.apply(test);
		
		return -Math.abs(comparison.getSampleAverage().getDeviation(test).getSum()) / comparison.getSampleAverage().getSum();
	}
	
	private Transform getDeviationTransform()
	{
		return new Transform() {
			public Sample apply(Sample input) {
				return input.getLevelLengths(input.getAverage() / 10);
			}
		};
	}

	public double getAcceptanceTreshold() {
		return -0.25;
	}
}
